package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.service.impl.VendingMachineServiceImpl;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompleteHandler implements MsgHandler {
    @Autowired
    VendingMachineServiceImpl vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        if (taskCompleteContract == null){
            throw new LogicException("未获取到消息");
        }
        //如果是投放工单 将该售货机状态改为运营
        if (VMSystem.TASK_TYPE_DEPLOY == taskCompleteContract.getTaskType()){
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_RUNNING);
        }
        //如果是撤机工单，将售货机状态改为撤机
        if (VMSystem.TASK_TYPE_REVOKE == taskCompleteContract.getTaskType()){
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_REVOKE);
        }
    }
}
